﻿@inject IJSRuntime JSRuntime
@inject NavigationManager NavigationManager

<DxButton Click="@OnToggleButtonClick"
          RenderStyle="ButtonRenderStyle.Secondary"
          IconCssClass="navbar-toggler-icon"
          CssClass="navbar-toggler d-block border-0"
          aria-label="@ButtonText"
          aria-pressed="@((!Toggled).ToString())">
</DxButton>

@code {
    bool _canToggle;
    [Parameter]
    public bool Toggled { get; set; }
    [Parameter]
    public EventCallback<bool> ToggledChanged { get; set; }

    async Task OnToggleButtonClick() => await ToggleState(!Toggled);
    public async Task ToggleState(bool isToggled) {
        if(_canToggle && Toggled != isToggled) {
            Toggled = isToggled;
            await ToggledChanged.InvokeAsync(Toggled);
        }
    }

    protected override void OnAfterRender(bool firstRender) {
        base.OnAfterRender(firstRender);
        _canToggle = true;
    }



    string ButtonText => Toggled ? "Open sidebar" : "Close sidebar";
}
